﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace web.admin
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{language}/{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "access_page", // Route name
                "{language}/{controller}/{action}/{id}", // URL with parameters
                new { controller = "account", action = "access_page", group_id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "employee_family", // Route name
                "{language}/{controller}/{action}/{employee_id}/{id}", // URL with parameters
                new { controller = "h_content", action = "edit_employee_family", employee_id = UrlParameter.Optional, id = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
               "leave_manage", // Route name
               "{language}/{controller}/{action}/{employee_id}/{id}", // URL with parameters
               new { controller = "h_content", action = "list_leave_manage", employee_id = UrlParameter.Optional, id = UrlParameter.Optional } // Parameter defaults
           );
            routes.MapRoute(
               "employee_foreign_countries", // Route name
               "{language}/{controller}/{action}/{employee_id}/{id}", // URL with parameters
               new { controller = "h_content", action = "list_employee_foreign_countries", employee_id = UrlParameter.Optional, id = UrlParameter.Optional } // Parameter defaults
           );
            routes.MapRoute(
              "delete_employee_medical_records", // Route name
              "{language}/{controller}/{action}/{employee_id}/{id}", // URL with parameters
              new { controller = "profile", action = "delete_employee_medical_records", employee_id = UrlParameter.Optional, id = UrlParameter.Optional } // Parameter defaults
          );
            routes.MapRoute(
                "dashboard",
                "{language}/dashboard/index",
                new { controller = "dashboard", action = "index" }
                );

            routes.MapRoute(
               "list_user",
               "{language}/account/user/{id}",
               new { controller = "account", action = "list_user", id = UrlParameter.Optional }
               );

            routes.MapRoute(
                "login",
                "{language}/account/login",
                new { controller = "account", action = "login" }
                );

            routes.MapRoute(
                "error404",
                "{language}/public/error404",
                new { controller = "public", action = "error404" }
                );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            if (Context.IsCustomErrorEnabled)
                ShowCustomErrorPage(Server.GetLastError());
        }

        private void ShowCustomErrorPage(Exception exception)
        {
            HttpException httpException = exception as HttpException;
            if (httpException == null)
                httpException = new HttpException(500, "Internal Server Error", exception);

            Response.Clear();
            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", "public");
            routeData.Values.Add("fromAppErrorEvent", true);

            switch (httpException.GetHttpCode())
            {
                case 403:
                    routeData.Values.Add("action", "error500");
                    break;

                case 404:
                    routeData.Values.Add("action", "error404");
                    break;

                case 500:
                    routeData.Values.Add("action", "error500");
                    break;

                default:
                    routeData.Values.Add("action", "OtherHttpStatusCode");
                    routeData.Values.Add("httpStatusCode", httpException.GetHttpCode());
                    break;
            }

            Server.ClearError();

            IController controller = new web.core.Controllers.publicController();
            controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
        }
    }
}